﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MySql.Data.MySqlClient;
using System.Reflection;
using MySqlManager.DatabaseBuilder;

namespace MySqlManager
{
    /// <summary>
    /// Class containing the data required to connect to a MySQL database
    /// </summary>
    public class MySqlCore
    {
        /// <summary>
        /// Host/UserID used to connect to MySql, default = "root"
        /// </summary>
        public static string UserID = "root";
        
        /// <summary>
        /// Password needed to connect to the host
        /// </summary>
        public static string Password;
        
        /// <summary>
        /// Name of the database
        /// </summary>
        public static string Database;
        
        /// <summary>
        /// True if use of the connection pool is required, default = false
        /// </summary>
        public static bool UsePooling = false;
        
        /// <summary>
        /// Gets or sets the minimum of connections that will be in the connection pool
        /// </summary>
        public static uint MinPoolCount { get; set; }
        
        /// <summary>
        /// Gets or sets the maximum of connections that wil be in the connection pool
        /// </summary>
        public static uint MaxPoolCount { get; set; }
        
        /// <summary>
        /// Sets the connection protocol used in the connections, default = Tcp
        /// </summary>
        public static MySqlConnectionProtocol ConnectionProtocol = MySqlConnectionProtocol.Tcp;
        
        /// <summary>
        /// Sets the name for the pipe if the ConnectionProtocol is 'Pipe'
        /// </summary>
        public static string PipeName;
        
        /// <summary>
        /// Gets the connection string usend in this MySqlCore
        /// </summary>
        public static string ConnectionString { get; private set; }
        
        /// <summary>
        /// Gets a new MySQL connection using the specified information
        /// </summary>
        public static MySqlConnection GetConnection()
        {
            if (string.IsNullOrEmpty(ConnectionString))
                CreateConnectionString();
            MySqlConnection connection = new MySqlConnection(ConnectionString);
            try
            {
                connection.Open();
                return connection;
            }
            catch
            {
                if (connection != null)
                    connection.Dispose(); 
                throw;
            }
        }

        /// <summary>
        /// Creates the MySQL connection string with the information in the MySqlCore class
        /// </summary>
        private static void CreateConnectionString()
        {
            MySqlConnectionStringBuilder builder = new MySqlConnectionStringBuilder();

            builder.UserID = UserID;
            builder.Password = Password;
            builder.Database = Database;
            if (ConnectionProtocol == MySqlConnectionProtocol.Tcp)
                builder.ConnectionProtocol = MySql.Data.MySqlClient.MySqlConnectionProtocol.Tcp;
            else
            {
                builder.ConnectionProtocol = MySql.Data.MySqlClient.MySqlConnectionProtocol.Pipe;
                builder.PipeName = PipeName;
            }

            builder.Pooling = UsePooling;
            builder.MaximumPoolSize = MaxPoolCount;
            builder.MinimumPoolSize = MinPoolCount;
            ConnectionString = builder.GetConnectionString(true);
        }
    }
}
